package com.sx.frame.tools;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.List;


/**4597520
 * 4665634
 * 使用说明：把旧的东西放在2目录上,生成文件在1中，执行即可
 */
public class DeleteDuplicateTorrentFile {
    public static String path = "E:\\test\\";
    public static String recordTxt = "downloadTorrent.txt";
    public static String recordTxt1 = "downloadTorrent1.txt";
    public static String oldTorrentPath = "2\\";
    public static String newTorrentPath = "1\\";

    public static void main(String[] args) {
        String regex = "^w\\d{3}$";
        File oldTorrent = new File(path+oldTorrentPath);
        List<String> exitsFileName = readFile(path + recordTxt);
        for (File f : oldTorrent.listFiles()){
            String oldName = processFileName(f.getName());
            if (!exitsFileName.contains(oldName) || oldName.matches(regex)){
                removeFile(f.getName());
                if (!exitsFileName.contains(oldName)){
                    exitsFileName.add(oldName);
                }
            }
        }
        try (BufferedWriter bw = new BufferedWriter(new FileWriter(path + recordTxt))) {
            for (String line : exitsFileName) {
                bw.write(line);
                bw.newLine(); // 添加换行符
            }
            System.out.println("文件写入成功");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    public static List<String> readFile(String path){
        List<String> exits =  new ArrayList<>();
        try (BufferedReader br = new BufferedReader(new FileReader(path))) {
            String line;
            while ((line = br.readLine()) != null) {
                String s = processFileName(line);
                exits.add(s);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return exits;
    }

    //处理文件中的名字
    public static String processFileName(String fileName) {
        fileName = fileName.toUpperCase().replaceAll("-UNCENSORED", "").replaceAll("-HD","").replaceAll("-AI","");
        // 返回处理后的文件名
        String result ;
        String[] split = fileName.split("-");
        if (split.length < 3){
            if (split.length < 2){
                result = split[0];
            }else {
                result =  split[0]+"-"+split[1];
            }
        }else {
            result =  split[0]+"-"+split[1]+"-"+split[2];
        }
        result = result.replace("-","").replace(" ","");
        return result;
    }

    public static void removeFile( String name){
        // 定义源文件路径
        Path source = Paths.get(path+oldTorrentPath+name);
        // 定义目标文件路径
        Path destination = Paths.get(path+newTorrentPath+name);

        try {
            // 移动文件
            Files.move(source, destination, StandardCopyOption.REPLACE_EXISTING);
            System.out.println("文件"+name+"移动成功！");
        } catch (IOException e) {
            // 处理异常
            System.err.println("移动文件时发生错误: " + e.getMessage());
        }
    }
}
